package com.store.app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.store.app.BaseActivity;
import com.store.app.ExitApplication;
import com.store.app.R;
import com.store.app.bean.CouponUpdateBean;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;
import com.store.app.utils.UserTokenUtils;

import org.json.JSONException;
import org.json.JSONObject;

import de.greenrobot.event.EventBus;

/**
 * 卡券页面
 */
public class CardVoucherActivity extends BaseActivity implements View.OnClickListener,IOnHttpListener {
    private TextView tvTitle;
    private RelativeLayout rl_faxing_card;      //发行优惠券
    private RelativeLayout rl_yanzheng_card;    //验证优惠券
    private TextView tv_faxing_number;          //发行数量
    private TextView tv_lingqu_number;          //领取数量
    private TextView tv_yanzheng_number;        //验证数量
    private NetRequest netRequest;
    private String total_onsell_num;            //发行
    private String total_verified_num;          //校验
    private String total_used_num;              //领取
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card_voucher);
        initView();
        initData();
        ExitApplication.getInstance().addActivity(this);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void initData(){
        netRequest = new NetRequest(this);
        UserTokenUtils.setUerToken(this);
        netRequest.requestCouponCount(1);
    }
    private void initView() {
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        tvTitle.setText("优惠券");
        findViewById(R.id.public_ll_return).setOnClickListener(this);
        rl_faxing_card = (RelativeLayout) findViewById(R.id.rl_faxing_card);
        rl_faxing_card.setOnClickListener(this);
        rl_yanzheng_card = (RelativeLayout) findViewById(R.id.rl_yanzheng_card);
        rl_yanzheng_card.setOnClickListener(this);
        tv_faxing_number = (TextView) findViewById(R.id.tv_faxing_number);
        tv_lingqu_number = (TextView) findViewById(R.id.tv_lingqu_number);
        tv_yanzheng_number = (TextView) findViewById(R.id.tv_yanzheng_number);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.public_ll_return:
                finish();
                break;
            case R.id.rl_faxing_card:
                startActivity(new Intent(CardVoucherActivity.this,CouponActivity.class));
                break;
            case R.id.rl_yanzheng_card:
                startActivity(new Intent(CardVoucherActivity.this,ValidateCouponActivity.class));
                break;
        }
    }

    @Override
    public void onAfinalFail() {

    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        if(requestId == 1){
            try {
                JSONObject jsonObject = new JSONObject(result);
                total_onsell_num = jsonObject.getString("total_onsell_num");
                total_verified_num = jsonObject.getString("total_verified_num");
                total_used_num = jsonObject.getString("total_used_num");
                if(!TextUtils.isEmpty(total_onsell_num)){
                    tv_faxing_number.setText(total_onsell_num);
                }
                if(!TextUtils.isEmpty(total_verified_num)){
                    tv_yanzheng_number.setText(total_verified_num);
                }
                if(!TextUtils.isEmpty(total_used_num)){
                    tv_lingqu_number.setText(total_used_num);
                }
            }catch (JSONException e){
                e.printStackTrace();
            }

        }
    }

    @Override
    public void onExecuteFail(int requestId, String msg) {

    }
    public void onEvent(CouponUpdateBean event) {

        if(event.type==1){
            Log.v("zyl","onEvent:requestCouponCount");
            netRequest.requestCouponCount(1);
        }
        if(event.type==2){
            Log.v("zyl","onEvent:requestCouponCount");
            netRequest.requestCouponCount(1);
        }
    }
}
